package org.moyu.handler;

import lombok.extern.slf4j.Slf4j;
import org.moyu.exception.SignalException;
import org.moyu.response.ErrorResponseData;
import org.moyu.response.ResponseData;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 拦截指纹异常
     * @param e 异常
     * @return 响应
     */
    @ExceptionHandler(SignalException.class)
    public ResponseEntity<ResponseData> handle400(SignalException e) {
        log.error("指纹拦截", e);
        return ResponseEntity.status(HttpStatus.OK).body(new ErrorResponseData(HttpStatus.BAD_REQUEST.value(), e.getLocalizedMessage(), null));
    }

    /**
     * 全局异常
     * @param e 异常
     * @return 响应
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseData> handle500(Exception e) {
        log.error("全局异常", e);
        return ResponseEntity.status(HttpStatus.OK).body(new ErrorResponseData(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), e));
    }

}
